package com.ruoyi.system.service.impl;

import com.github.pagehelper.PageInfo;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.core.page.PageInFoDomain;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.domain.ElderlyFallRecord;
import com.ruoyi.system.mapper.ElderlyFallRecordMapper;
import com.ruoyi.system.service.IElderlyFallRecordService;
import com.ruoyi.system.service.ISysUserService;
import com.ruoyi.system.utils.PageHelperUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

/**
 * @author qiy
 */
@Service
@RequiredArgsConstructor
public class ElderlyFallRecordImpl implements IElderlyFallRecordService {

    private final ElderlyFallRecordMapper elderlyFallRecordMapper;

    private final ISysUserService sysUserService;

    @Override
    public PageInFoDomain<ElderlyFallRecord> list(ElderlyFallRecord record) {
        PageHelperUtils.startPage(record.getPageNum(), record.getPageSize());
        List<ElderlyFallRecord> elderlyFallRecords = selectList();
        PageInfo<ElderlyFallRecord> pageInfo = new PageInfo<>(elderlyFallRecords);
        PageInFoDomain<ElderlyFallRecord> pageInFoDomain = new PageInFoDomain<>();
        // 设置分页信息
        pageInFoDomain.setPageNum(pageInfo.getPageNum());
        pageInFoDomain.setRows(pageInfo.getList());
        pageInFoDomain.setTotal(pageInfo.getTotal());
        return pageInFoDomain;
    }

    @Override
    public ElderlyFallRecord selectById(Long id) {
        return elderlyFallRecordMapper.selectByPrimaryKey(id);
    }

    @Override
    public int insert(ElderlyFallRecord record) {
        setElderlyDallRecordInsert(record);
        return elderlyFallRecordMapper.insertSelective(record);
    }

    @Override
    public int end(ElderlyFallRecord record) {
        String reqId = record.getReqId();
        // 结束跌倒记录
        Example example = new Example(ElderlyFallRecord.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("reqId", reqId);
        List<ElderlyFallRecord> elderlyFallRecords = elderlyFallRecordMapper.selectByExample(example);
        if (!elderlyFallRecords.isEmpty()){
            ElderlyFallRecord elderlyFallRecord = elderlyFallRecords.get(0);
            elderlyFallRecord.setFallEndTime(DateUtils.getNowDate());
            return elderlyFallRecordMapper.updateByPrimaryKeySelective(elderlyFallRecord);
        }
        return 0;
    }

    private void setElderlyDallRecordInsert(ElderlyFallRecord record) {
        record.setDelFlag("0");
        //老人ID
        record.setElderlyId(102L);
        SysUser sysUser = sysUserService.selectUserById(record.getElderlyId());
        //老人姓名
        record.setElderlyName(sysUser.getUserName());
        //跌倒开始时间
        record.setFallStartTime(DateUtils.getNowDate());
        //跌倒类型
        record.setFallType("3");
        //严重程度
        record.setSeverityLevel("1");
        //报警状态
        record.setAlertStatus("1");
        //是否受伤
        record.setIsInjury("1");
        //状态
        record.setStatus("0");
        //创建者
        record.setCreateBy("admin");
        //创建时间
        record.setCreateTime(DateUtils.getNowDate());
    }

    private List<ElderlyFallRecord> selectList() {
        Example example = new Example(ElderlyFallRecord.class);
        example.orderBy("createTime").desc();
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("delFlag","0");
        return elderlyFallRecordMapper.selectByExample(example);
    }
}
